package com.dunqi.gpm.cangxi.WeekReport

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import  com.dunqi.gpm.cangxi.WeekReport.WeekPlan.ItemWeekPlanListFragment
import  com.dunqi.gpm.cangxi.WeekReport.WeekReport.ItemWeekReportListFragment
import  com.dunqi.gpm.cangxi.WeekReport.WeekChek.ItemWeekCheckFragment
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import com.dunqi.gpm.cangxi.R

import java.util.ArrayList

import butterknife.ButterKnife
import butterknife.Unbinder
import com.dunqi.gpm.cangxi.WeekReport.Model.WeekChekModel
import com.dunqi.gpm.cangxi.WeekReport.Model.WeekPlan
import com.dunqi.gpm.cangxi.WeekReport.Model.WeekReport
import com.dunqi.gpm.cangxi.WeekReport.WeekChek.WeekCheckDetail.WeekCheckDetailFragment
import com.dunqi.gpm.cangxi.home.adapter.ViewPagerFragmentAdapter
import com.dunqi.gpm.cangxi.common.util.LogUtils
import kotlinx.android.synthetic.main.activity_test.*

class WeekReportListControlerAcitivty : AppCompatActivity(), OnListFragmentInteractionListener {

    override fun onListFragmentInteraction(item: Any, flag: Int) {
        startReportDatail(flag, item)
    }

    lateinit var bind: Unbinder
    internal var fragments = ArrayList<Fragment>()
    val titles = ArrayList<String>() //"周报配置",
    var index: Int = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
        bind = ButterKnife.bind(this)

        val mType = intent.getStringExtra("type")
        val menus = intent.getStringExtra("menus")
        when (mType) {
        /**周计划*/
            "zbg_zjh" -> {
                title_week_report.setRightText("添加") {
                    startReportDetail(WeekPlan)
                }
                title_week_report.setTitle("周计划")
                titles.add("已计划")
                fragments.add(ItemWeekPlanListFragment.newInstance(1))
                titles.add("已报告")
                fragments.add(ItemWeekPlanListFragment.newInstance(2))
            }
        /**周报告*/
            "zbg_zbg" -> {
                title_week_report.setTitle("周报告")
                title_week_report.setRightText("添加") {
                    startReportDetail(WeekReport)
                }
                titles.add("待审核")
                fragments.add(ItemWeekReportListFragment.newInstance(1))
                titles.add("已审核")
                fragments.add(ItemWeekReportListFragment.newInstance(2))
            }
        /**周报审核*/
            "zbg_zbsh" -> {
                title_week_report.setTitle("周报审核")
                titles.add("待审核")
                titles.add("审核完成")
                fragments.add(ItemWeekCheckFragment.newInstance(WeekCheckDetailFragment.CanEdit))
                fragments.add(ItemWeekCheckFragment.newInstance(WeekCheckDetailFragment.LookInfo))
            }
        }
        val title_show = arrayOfNulls<String>(titles.size)
        for (i in titles.indices) {
            title_show[i] = titles.get(i)
        }
        vp_week.adapter = ViewPagerFragmentAdapter(supportFragmentManager, fragments, title_show)
        tl_week.setupWithViewPager(vp_week)
        tl_week.getTabAt(index)?.select()
        getSystemService(Context.LAYOUT_INFLATER_SERVICE)
    }

    override fun onDestroy() {
        super.onDestroy()
        bind.unbind()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
            }
        }
    }

    var WeekSet = 1  // 1  周计划配置
    var WeekPlan = 2// 2  周计划
    var WeekReport = 3//3  周报告
    var WeekCheck = 4//4   周报审核
    fun startReportDetail(flag: Int) {
        startReportDatail(flag, null)
    }

    fun startReportDetail(item: Any) {
        startReportDatail(0, item)
    }

    fun startReportDatail(flag: Int, item: Any?) {
        var mIntent: Intent? = null
        if (item !== null) {
            if (item is WeekPlan) {
                mIntent = Intent(this@WeekReportListControlerAcitivty, WeekDetailControlerActivity::class.java)
                mIntent.putExtra("object", item)
                mIntent.putExtra("type", WeekPlan) // 1  周计划配置  / 2  周计划   /3  周报告  /4   周报审核
            } else if (item is WeekReport) {
                mIntent = Intent(this@WeekReportListControlerAcitivty, WeekDetailControlerActivity::class.java)
                mIntent.putExtra("object", item)
                mIntent.putExtra("type", WeekReport)
            } else if (item is WeekChekModel) {
                mIntent = Intent(this@WeekReportListControlerAcitivty, WeekDetailControlerActivity::class.java)
                mIntent.putExtra("object", item)
                mIntent.putExtra("flag_checktype", flag)
                mIntent.putExtra("type", WeekCheck)
            }
        } else {
            if (flag == 0)
                return
            when (flag) {
                WeekPlan -> {
                    mIntent = Intent(this@WeekReportListControlerAcitivty, WeekDetailControlerActivity::class.java)
                    mIntent.putExtra("type", WeekPlan)
                }
                WeekReport -> {
                    mIntent = Intent(this@WeekReportListControlerAcitivty, WeekDetailControlerActivity::class.java)
                    mIntent.putExtra("type", WeekReport)
                }
                WeekCheck -> { //不存在这种情况
                }
            }
        }
        if (mIntent !== null)
            startActivity(mIntent)
        else {
            LogUtils.w("WeekReportListControlerAcitivty", "mIntent 为空")
        }
    }
}
